﻿//======================================================================
//
//        Copyright (C) 2015   
//        All rights reserved
//
//        filename : MakingBadCard
//        description : 制卡坏卡
//        created by XXX at  20160115
//
//======================================================================
using System;
using CardLogistics.Web.Components;
using System.Data;
using System.Text;
using CardLogistics.DAL;
using System.Web.UI.WebControls;

namespace CardLogistics.Web.MainPage.CardRecovery.MakingBadCard
{
    public partial class AddEdit : BasePage
    {
        private static readonly CardLogistics.DAL.CardRecovery.CARDRECOVERY_MakingBadData DAL = new DAL.CardRecovery.CARDRECOVERY_MakingBadData();
        private static readonly CardLogistics.DAL.CardRecovery.CARDRECOVERY_MakingBad_DetailData DDAL = new CardLogistics.DAL.CardRecovery.CARDRECOVERY_MakingBad_DetailData();
        private static readonly CardRecordData CardDAL = new CardRecordData();
        protected string IsSubmit = "0";//当前单据状态
        public static string TitleName;//显示的标题

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                RECOVERYDATE.Value = DateTime.Now.ToString("yyyy-MM-dd");//入库日期
                InitControl();
            }
        }

        #region 编辑时绑定值
        /// <summary>
        /// 编辑时绑定值
        /// </summary>
        public void InitControl()
        {
            TitleName = GetStatusInfo(0, "制卡坏卡");
            if (IsEdit)
            {
                string _key = Request.QueryString["id"];
                if (!string.IsNullOrEmpty(_key))
                {
                    CardLogistics.Model.CardRecovery.CARDRECOVERY_MakingBad pData = DAL.GetModel(_key);
                    SetWebControls(this.Page, pData);
                    DataTable dt = DDAL.GetInfoByRecoveryId(pData.ID);
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        rptDetailList.DataSource = dt;
                        //根据卡号升序排列
                        dt.DefaultView.Sort = "StartCardNo ASC,EndCardNo ASC";
                        rptDetailList.DataBind();
                    }
                    btn_Add.Visible = btn_Submit.Visible = false;
                    if (pData.IsSubmit == 0 || pData.IsSubmit == 3)//状态为 0(未提交) 或 3(驳回)时才可以进行编辑操作
                    {
                        btn_Add.Visible = btn_Submit.Visible = true;
                    }
                    TitleName = GetStatusInfo(int.Parse(pData.IsSubmit.ToString()), "制卡坏卡");
                    hf_PutInCardId.Value = pData.PUTINCARDID;
                    PutInCardNo.Value = pData.PUTINCARDNO;
                }
            }
            else
            {
                RECOVERYNO.Value = GetBillNumber("制卡坏卡");
            }

        }
        #endregion

        /// <summary>
        /// 保存按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Add_Click(object sender, EventArgs e)
        {
            try
            {
                string result = EncapsulationModel(0);
                ShowMegByResult(result, 0);
            }
            catch (Exception ex)
            {
                ShowMessageBox("系统异常:" + ex.ToString());
            }
        }
        /// <summary>
        /// 保存并提交
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Submit_Click(object sender, EventArgs e)
        {
            try
            {
                string result = EncapsulationModel(1);
                ShowMegByResult(result, 1);
            }
            catch (Exception ex)
            {
                ShowMessageBox("系统异常:" + ex.ToString());
            }
        }

        #region 提示信息
        /// <summary>
        /// 提示信息
        /// </summary>
        /// <param name="result"></param>
        private void ShowMegByResult(string result, int issubmit)
        {
            if (result.Split(',')[0] == "ok")
            {
                if (issubmit == 1)
                {
                    InsertAppInfo(11, result.Split(',')[1]); // 11制卡坏卡
                    InsertLog("保存并提交制卡坏卡信息,单据编号:" + RECOVERYNO.Value);
                }
                else
                    InsertLog("保存制卡坏卡信息,单据编号:" + RECOVERYNO.Value);
                ReturnListInfo();
            }
            else
            {
                ShowMessageBox("操作失败请重新操作！" + result.Split(',')[1]);
            }
        }
        #endregion

        /// <summary>
        /// 未售问题卡回收信息
        /// </summary>
        /// <param name="issubmit">是否提交</param>
        /// <returns></returns>
        private string EncapsulationModel(int issubmit)
        {
            string result = string.Empty;
            CardLogistics.Model.CardRecovery.CARDRECOVERY_MakingBad model = new Model.CardRecovery.CARDRECOVERY_MakingBad();
            model = GetWebControls<CardLogistics.Model.CardRecovery.CARDRECOVERY_MakingBad>(this.Page);
            model.LASTUPDATEUSER = LoggedUser.ID;
            model.LASTUPDATEDATE = DateTime.Now;
            model.IsSubmit = issubmit;
            model.PUTINCARDID = hf_PutInCardId.Value;
            string fileValue = "";
            foreach (RepeaterItem ri in this.rptDetailList.Items)
            {
                string cardNO = ((Label)ri.FindControl("lbl_STARTCARDNO")).Text;
                fileValue += cardNO + "|" + cardNO + "|1;";
            }

            if (IsEdit)
            {
                model.ID = Request.QueryString["id"];
                result = DAL.Update(model, fileValue.TrimEnd(';'));
            }
            else
            {
                result = DAL.Add(model, fileValue.TrimEnd(';'));
                if (result.Split(',')[0] == "ok")
                {
                    ChgBillNumber("制卡坏卡");
                }
            }
            return result;
        }

        protected void btn_PutInCardNo_Click(object sender, EventArgs e)
        {
            if (hf_PutInCardId.Value != "")
            {
                DataTable dt = CardDAL.GetZKHKByRKID(hf_PutInCardId.Value);
                rptDetailList.DataSource = dt;
                //根据卡号升序排列
                dt.DefaultView.Sort = "StartCardNo ASC,EndCardNo ASC";
                rptDetailList.DataBind();
            }
        }

    }
}